package com.wangxiaoxuan.seckill.rabbitmq;

import com.alibaba.fastjson2.JSON;
import com.wangxiaoxuan.seckill.pojo.Order;
import com.wangxiaoxuan.seckill.pojo.SeckillGoods;
import com.wangxiaoxuan.seckill.pojo.SeckillOrder;
import com.wangxiaoxuan.seckill.service.IOrderService;
import com.wangxiaoxuan.seckill.service.ISeckillGoodsService;
import com.wangxiaoxuan.seckill.service.ISeckillOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @Description:消息监听者
 * @Author：wangxiaoxuan
 * @Date
 **/
@Service
@Slf4j
public class MQListener {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 接收保存订单信息到mysql
     * @param map
     *  order:订单信息
     *  seckillOrder：秒杀订单信息，但其中的订单id未添加
     */
    @RabbitListener(queues = "queue_seckill_order")
    public void saveOrder(HashMap<String,String> map){
        //分别获取order信息和seckillOrder信息
        String orderJson = map.get("order");
        String seckillOrderJson = map.get("seckillOrder");

        //将order订单保存到mysql中,并且获得orderId
        Order order = com.alibaba.fastjson.JSON.parseObject(orderJson,Order.class);
        Long goodsId = (Long) order.getGoodsId();
        //订单表插入之后，返回主键
        Long orderId;
        try {
            orderId = orderService.insertOrder(order);
        }catch (Exception e){
            //若订单插入失败，那么需要将redis中的库存进行自增1，达到缓存一致
            redisTemplate.opsForValue().increment(goodsId);
            return;
        }

        //获取到秒杀订单
        SeckillOrder seckillOrder = com.alibaba.fastjson.JSON.parseObject(seckillOrderJson, SeckillOrder.class);
        //将秒杀订单中的订单id补全
        seckillOrder.setOrderId(orderId);
        seckillOrderService.insertSeckillOrder(seckillOrder);
    }

    /**
     * 秒杀成功后库存减1
     * @param goodsId
     */
    @RabbitListener(queues = "queue_seckill_stock")
    public void decreaseStock(String goodsId){
        //在redis中获取秒杀的商品
        SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.opsForValue().get("seckillGoods" + ":" + goodsId);
        seckillGoods.setStockCount(0);
        //log.info(seckillGoods.toString());
        seckillGoodsService.decreaseStock(seckillGoods);
    }

    @RabbitListener(queues = "queue_seckill_dlx")
    public void checkOrder(String orderKey){
        //先从redis中获取这个订单信息
        Order order=(Order) redisTemplate.opsForValue().get(orderKey);
        //判断订单的支付状态
        //0：未支付  1：已支付
        if(order.getStatus()==0){
            //未支付，那么取消订单，库存自增
            redisTemplate.delete(orderKey);
            redisTemplate.opsForValue().increment("seckillGoodsStock:"+order.getGoodsId());
            //删除数据库的订单信息
            orderService.deleteOrderTimeOut(order);
        }
    }

    /**
     * 监听支付情况,支付成功数据同步到数据库
     * @param map
     */
    @RabbitListener(queues = "queue_seckill_pay")
    public void pay(Map<String,String> map){
        orderService.updatePayStatus(map);
    }
}
